package com.eascs.admin.o2o.notify.dubbo;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.eascs.admin.common.service.IRabbitMsgRecordService;
import com.eascs.admin.o2o.loan.notify.common.O2OWFService;
import com.eascs.app.log.Logger;
import com.eascs.app.log.LoggerFactory;
import com.eascs.workflow.common.vo.ModelResult;
import com.eascs.workflow.common.vo.WFNotifyParam;
import com.eascs.admin.o2o.notify.dao.WFStatusDao;
import com.eascs.admin.o2o.notify.entity.RabbitMsgRecord;

@Service
public class RabbitMsgRecordTask implements IRabbitMsgRecordService {
	protected final Logger logger = LoggerFactory.getLogger(getClass());
	@Autowired
	private WFStatusDao WFStatusDao;
	@Autowired
	private O2OWFService wfService;

	@Override
	public com.eascs.admin.common.vo.ModelResult<Boolean> mtc() {
		logger.info("RabbitMsgRecordTask Start [消息事务补偿]");
		com.eascs.admin.common.vo.ModelResult<Boolean> modle = new com.eascs.admin.common.vo.ModelResult<>();
		new Thread(new Runnable() {
			@Override
			public void run() {
				try {
					ModelResult<Boolean> modelResult = new ModelResult<Boolean>();
					List<RabbitMsgRecord> list = WFStatusDao.queryRabbitMsgRecordList();
					logger.info("list size [{}]", list.size());
					for (RabbitMsgRecord record : list) {
						try {
							logger.info("RabbitMsgRecord msgId:[{}],content:[{}]", record.getMsgId(), record.getContent());
							WFNotifyParam notifyParam = JSON.parseObject(record.getContent(), WFNotifyParam.class);
							modelResult = wfService.wfNotify(notifyParam);
							if (modelResult.isSuccess()) {
								WFStatusDao.rabbitMsgSucc(record.getUniqueCode());
							}
							logger.info("wfNotify result:[{}]", modelResult.isSuccess());
						} catch (Exception e) {
							logger.info("RabbitMsgRecord exec err.", e);
						}
					}
				} catch (Exception e) {
					logger.error("归档定时导出数据生产失败，message：{},stack：{}", e.getMessage(), e.getStackTrace());
				}
				logger.info("RabbitMsgRecordTask End [消息事务补偿]");
			}
		}).start();

		return modle.withModel(Boolean.TRUE);

	}

}
